<?php

abstract class CBaseInputData
{
	/**
	 * @param string $mValue
	 * @return string
	 */
	protected function cleanValue($mValue)
	{
		return $mValue;
	}
}

class CInputData extends CBaseInputData
{
	/**
	 * @var bool
	 */
	private $_bMagicQuotesOn;

	/**
	 * @return CPostInputData
	 */
	public function __construct()
	{
		$this->_bMagicQuotesOn = (bool) @get_magic_quotes_gpc();
	}

	/**
	 * @param string $sKey
	 * @param mixed $nmDefault = null
	 * @return mixed
	 */
	public function GetCookieValue($sKey, $nmDefault = null)
	{
		return (isset($_COOKIE[$sKey])) ? $this->cleanValue($_COOKIE[$sKey]) : $nmDefault;
	}

	/**
	 * @param string $sKey
	 * @param mixed $nmDefault = null
	 * @return mixed
	 */
	public function GetPostValue($sKey, $nmDefault = null)
	{
		return (isset($_POST[$sKey])) ? $this->cleanValue($_POST[$sKey]) : $nmDefault;
	}

	/**
	 * @param string $sKey
	 * @param mixed $nmDefault = null
	 * @return mixed
	 */
	public function GetQueryValue($sKey, $nmDefault = null)
	{
		return (isset($_GET[$sKey])) ? $this->cleanValue($_GET[$sKey]) : $nmDefault;
	}

	/**
	 * @param string $mValue
	 * @return string
	 */
	protected function cleanValue($mValue)
	{
		if (!$this->_bMagicQuotesOn)
		{
			return $mValue;
		}

		$mResult = null;
        if (is_array($mValue))
        {
			if (0 < count($mValue))
			{
				$mResult = array();
				foreach ($mValue as $sArrayKey => $mArrayValue)
				{
					$mResult[$sArrayKey] = $this->cleanValue($mArrayValue);
				}
			}
        }
        else
        {
            $mResult = stripslashes($mValue);
        }
		
        return $mResult;
	}
}